//
//  WaterPullDataHandle.m
//  MarryMe
//
//  Created by lanou3g on 16/1/7.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "WaterPullDataHandle.h"
#import "AFNetworking.h"
#import "Homemodel.h"
static WaterPullDataHandle*_waterPullDataHandle =nil;
@implementation WaterPullDataHandle
+(instancetype)shareWaterPullDataHandle{
    return [[self alloc]init];
    
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
  _waterPullDataHandle= [super allocWithZone:zone];
        
    });
    return _waterPullDataHandle;
    
}

-(void)getDataFromNet1:(ReloadDataBlock)finisheBlock{
    //创建管理类
    AFHTTPSessionManager *sessionManger = [AFHTTPSessionManager manager];
    //设置请求格式
    sessionManger.requestSerializer = [AFHTTPRequestSerializer serializer];
    //设置返回格式
    sessionManger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [sessionManger GET:self.urlstr parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSString *str  =  [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSData *resData = [[NSData alloc] initWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
        
        //系统自带JSON解析
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
        
        NSArray  *dataDic = [resultDic valueForKey:@"data"];
        NSMutableArray * giftArray = [NSMutableArray array];
        for (NSDictionary *dic in dataDic) {
            Homemodel*model  = [[Homemodel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [giftArray addObject:model];
    
        }
        self.waterPullDataArray = [NSArray arrayWithArray:giftArray];
        NSLog(@"请求成功");
        dispatch_async(dispatch_get_main_queue(), ^{
            
            finisheBlock(YES);
            
        });
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败");
    }];
    
    
    
    
}

@end
